package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {

    @Autowired
    AuthorService authorService;

    /**
     * 根据用户id查询作者信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/findByUserId/{userId}")
    @Override
    public ApAuthor findByUserId(Integer userId) {

        return authorService.getOne(Wrappers.<ApAuthor>lambdaQuery()
                .eq(ApAuthor::getId,userId));
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult save(ApAuthor apAuthor) {
        authorService.save(apAuthor);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据Id查询作者
     *
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findById(@PathVariable Integer id) {
        return authorService.getById(id);

    }
}
